﻿using System.ComponentModel;
using System.Drawing;
using Newtonsoft.Json;
using ProjectN.Core.Element.Def;
using ProjectN.Core.Instance.Data;

namespace ProjectN.Core.Element
{
    static partial class Utilities
    {
        public static Pen ColoredPen(Pen origin, Color? color)
        {
            if (color.HasValue)
                return new Pen(color.Value, origin.Width);
            else
                return origin;
        }
    }

    [JsonObject(MemberSerialization.OptIn)]
    public class Line : BasicElement
    {
        [Category("基本"), DisplayName("画笔"), JsonProperty]
        public required PenInfo Pen { get; set; }

        [Category("基本"), DisplayName("点 1"), JsonProperty]
        public required Point P1 { get; set; }

        [Category("基本"), DisplayName("点 2"), JsonProperty]
        public required Point P2 { get; set; }
        public Line()
        {
            this.Draw += Line_Draw;
            this.Load += Line_Load;
        }

        private void Line_Draw(object sender, DrawEventArgs e)
        {
            e.Graphics.DrawLine(Utilities.ColoredPen(Pen.Create(), e.Color), P1, P2);
        }

        private void Line_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Pen.Data = data;
        }

        public override string ToString() => "线段";
    }

    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class LineBatch : BasicElement
    {
        [Category("基本"), DisplayName("画笔"), JsonProperty]
        public required PenInfo Pen { get; set; }

        [Category("基本"), DisplayName("关键点"), JsonProperty]
        public required Point[] Points { get; set; }
        public LineBatch()
        {
            this.Draw += LineBatch_Draw;
            this.Load += LineBatch_Load;
        }

        private void LineBatch_Draw(object sender, DrawEventArgs e)
        {
            e.Graphics.DrawLines(Utilities.ColoredPen(Pen.Create(), e.Color), Points);
        }

        private void LineBatch_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Pen.Data = data;
        }

        public override string ToString() => "线段组";
    }
}
